﻿using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace TemplateAddIn
{
    /// <summary>
    /// This delegate defines a pattern for events for Variables
    /// </summary>
    /// <param name="sender">The variable that invoked the event.</param>
    public delegate void VariableEvent(Variable sender);

    public abstract class Variable
    {
        #region Fields
        private string name;
        private string fullName;
        private List<Word.ContentControl> contentControlList;
        private Word.WdContentControlType variableType;

        protected object content;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the name of the variable defined by the user.
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Gets the Full name of the variable, which specifies the type of the variable.
        /// </summary>
        public abstract string FullName
        {
            get;
        }

        /// <summary>
        /// Gets the content of the variable.
        /// </summary>
        public object Content
        {
            get { return content; }
        }

        /// <summary>
        /// Gets a list containing the Content Controls associated with the variable.
        /// </summary>
        public List<Word.ContentControl> ContentControlList
        {
            get
            {
                if (this.contentControlList == null)
                    this.contentControlList = new List<Word.ContentControl>();

                return contentControlList;
            }
        }

        /// <summary>
        /// Gets the type of the variable. 
        /// </summary>
        public Word.WdContentControlType VariableType
        {
            get { return variableType; }
        }
        #endregion

        #region Events
        public event VariableEvent ContentChanged;
        #endregion

        #region Abstract Methods
        /// <summary>
        /// Sets the content of the variable.
        /// </summary>
        /// <param name="pContent">An object containing the content to be set in the variable.</param>
        /// <returns>A bool indicating whether the variable content have changed or not.</returns>
        public abstract bool SetContent(object pContent);

        /// <summary>
        /// This method updates the content of all Content Controls associated with the calling variable.
        /// </summary>
        /// <param name="var">The variable that invoked the event.</param>
        protected abstract void Variable_ContentChanged(Variable sender);
        #endregion

        #region Invoke Methods
        protected virtual void RaiseContentChanged()
        {
            this.ContentChanged(this);
        }
        #endregion

        #region Constructors
        protected Variable(string pName, Word.WdContentControlType pVariableType)
        {
            this.name = pName;
            this.variableType = pVariableType;

            this.ContentChanged += new VariableEvent(Variable_ContentChanged);
        }
        #endregion
    }
}
